home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
vol_200
/
203_01
/
yam7.c
< prev
next >
Wrap
Text File
|
1980-01-01
|
39KB
|
1,623 lines
/*
$title ('yam7.c: file i/o, initialization')
$date (11 NOV 85)
*/
/*
* File open and close stuff and more ...
* This file has been modified for operation on a MS-DOS system
*/
#include <dos.h>
#include "yam.h"
#include <fcntl.h>
/* globals used by dodir */
unsigned Numfiles; /* Total number of files expanded */
unsigned Blocks; /* Number of records */
/* following are MSDOS system calls */
#define COMPFILSIZ 0x23 /* compute file size */
#define CHANGEDIR 0x3B /* change current directory */
#define FINDFIRST 0x4E /* bdos search for file pattern*/
#define FINDNEXT 0x4F /* search for next occurrence */
#define GETDEFDISK 0x19 /* get current disk */
#define GETFREE 0x36 /* get free disk space */
#define SELDSK 0x0E /* bdos select disk 0=default disk */
#define SETDMA 0x1A /* set address for read, write, etc. */
#define DIRAT 0x10 /* directory attribute for file searches */
#define ARCHIVE 0x20 /* archive bit */
#define NOTFOUND 18 /* file not found in search */
#define CHATTRIB 0x43 /* change attribute */
#define CARRYFLG 0x01
extern char *getenv();
/****************************************************************************
FUNCTION:
get an single char from console. Display it as entered.
CALLING PARAMETERS:
none
RETURNED PARAMETERS:
character value typed on console
===========================================================================*/
char getopt()
{
char c;
/* note: if <ctype.h> included, tolower lattice c function makes
this routine request double entrys for each char. */
c = tolower(getcty());
putcty(c);
return c;
} /* getopt */
/****************************************************************************
FUNCTION:
open a file for receive. This file will be written to.
CALLING PARAMETERS:
pathname:
pointer to ascii file pathname representation
===========================================================================*/
openrx(pathname)
char *pathname;
{
char option;
#ifdef RESTRICTED
char *s;
/* no garbage names please */
for(s=pathname; *s; )
if(*s++ > 'z')
return ERROR;
#endif
unspace(pathname);
/* show the name right away */
printf("'%s' ", pathname);
/* open file for read. If exists, allow option to overwrite */
if(!Creamfile && ((fout=fopen(pathname,"r")) != NULL))
{
/* close file if successful open */
fclose(fout);
#ifdef XMODEM
printf("Can't upload: file exists\n");
return ERROR;
#else
printf("Exists, Replace/Append/Quit (r/a/q)?");
option=getopt();
if(!index(option, "ary"))
return ERROR;
#endif
}
if(option=='a')
fout=fopen(pathname,"ab+");
else
fout=fopen(pathname,"wb");
if (fout==NULL)
{
printf(" Can't create\n");
return ERROR;
}
/* receive file is open */
Rfile= TRUE;
strcpy(Rname, pathname);
/* Dumping means term input will go to disk. Squelch allows this
to be turned on and off. Why I don't know. */
Dumping = !Squelch;
printf(" Open\n");
#ifdef STATLINE
lpstat("Receiving %s", Rname);
#endif
return OK;
}
/****************************************************************************
FUNCTION:
close receive data file
CALLING PARAMETERS:
pad:
sets pad for log file
===========================================================================*/
closerx(pad)
{
/* close only if sucessful open */
if(Rfile)
{
/* flush all buffers before closing */
fflush(fout);
fclose(fout);
Rfile=FALSE;
#ifdef LOGRX
logfile(LOGRX, Rname, pad?'*':'R'); /* record file xmsn */
#endif
}
}
/****************************************************************************
FUNCTION:
get file size and return number bytes occupied by file
CALLING PARAMETERS:
*pathname:
pointer to file pathname.
===========================================================================*/
long getfilesize(pathname)
char *pathname;
{
struct find_buf f_buf;
union REGS inregs;
union REGS outregs;
/* DMA must point to f_buf structure */
bdos(SETDMA, &f_buf);
/* set pointer to file name */
inregs.x.dx = (short)pathname;
/* find first DOS function */
inregs.h.ah = FINDFIRST;
/* allow search for files only */
inregs.x.cx = DIRAT;
/* dos request function */
intdos(&inregs,&outregs);
if (outregs.h.al != NULL)
return NULL;
/* return number of blocks used by file */
return f_buf.filesize;
} /* getfilesize */
/****************************************************************************
FUNCTION:
open a file for transmission to a remote
CALLING PARAMETERS:
mode:
0=don't compute file size
1=compute file size
>1=use file size already computed in f_buf
f_buf:
pointer to a structure containing information about file. if
0 routine will not report file transmission time. If 1, file
size must be calculated, otherwise the structure is already
initialized.
*pathname:
pointer to a string representing file name to be opened
===========================================================================*/
opentx(mode,f_buf,pathname)
struct find_buf *f_buf;
char *pathname;
int mode;
{
int blocks;
long bytes;
/* set file name into global buffer */
unspace(pathname);
strcpy(Tname, pathname);
printf("'%s' ", pathname);
if((fin=fopen(pathname,"rb"))==NULL)
{
printf("Can't open\n");
return ERROR;
}
printf("Open\n");
#ifdef RESTRICTED
/* restriced file stuff needs to be added */
fclose(fin);
printf("Not for Distribution\n");
return ERROR;
#endif
/* transmit file is open */
Tfile= TRUE;
/* compute file size. mode will <> NULL when f_buf it is initialized
by another routine */
if (mode == 1)
bytes = getfilesize(pathname);
else if (mode != NULL)
bytes = f_buf->filesize;
/* display download time */
blocks = (int)(bytes/blklen + (bytes%blklen?1:0));
if (mode != NULL)
dis_xmit_time(1,blocks);
return OK;
} /* opentx */
/****************************************************************************
FUNCTION:
display transmission time of a file
CALLING PARAMETERS:
nfiles:
number of file(s) to be sent
blks:
number of blks occupied by file(s)
===========================================================================*/
dis_xmit_time(nfiles,blks)
unsigned nfiles;
unsigned blks;
{
unsigned spm;
unsigned dminutes;
/* sector per minute */
/* (Baudrate*60)/(10 bits each char * 136 chars) */
spm = Baudrate/23;
/* download minutes calculation */
dminutes = nfiles+((10*(nfiles+blks))/spm)+(blks/20);
/* display computed transmission time */
/* display number and size. #k/s = 128b/s / 1024b/k = 1/8 */
printf("%u Blocks %dk %u.%u Minutes Xmsn Time at %u Baud\n",
blks, blks/8, dminutes/10, dminutes%10, Baudrate);
/* display file name, length and minutes for transfer on status line */
#ifdef STATLINE
lpstat("%s %u Blocks %dk %u.%u Min",
Tname, blks, blks/8, dminutes/10, dminutes%10);
#endif
} /* dis_xmit_time */
/****************************************************************************
FUNCTION:
closetx(status) call with status != 0 if incomplete file xmsn
CALLING PARAMETERS:
status:
if false and LOGTX, will call logfile with Tname
===========================================================================*/
closetx(status)
{
if(Tfile)
{
fclose(fin);
#ifdef LOGTX
if(!status)
logfile(LOGTX, Tname, 's'); /* record file xmsn */
#endif
Tfile=FALSE;
}
} /* closetx */
/****************************************************************************
FUNCTION:
search the phone file for name
CALLING PARAMETERS:
name:
pointer to name to search for in phone file
buffer:
pointer to buffer to use to read phone file into
RETURNED PARAMETERS:
TRUE if succesful, ERROR if something went wrong
===========================================================================*/
#ifdef PHONES
getphone(name, buffer)
char *name, *buffer;
{
closetx(TRUE);
searchpath(PHONES,Utility.ubuf);
if((fin=fopen(Utility.ubuf,"rb"))==NULL)
{
p